package com.hdd.biz.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdd.biz.account.entity.Account;
import com.hdd.biz.app.mapper.APPMemberMapper;
import com.hdd.biz.app.mapper.AppAccountMapper;
import com.hdd.biz.member.dto.*;
import com.hdd.biz.member.entity.Member;
import com.hdd.biz.member.mapper.MemberMapper;
import com.hdd.biz.member.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdd.biz.painting.service.PaintingWorksService;
import com.hdd.dto.SuperResponse;
import com.hdd.utils.DateUtil;
import com.hdd.utils.IdUtils;
import com.hdd.utils.wx.UserPnUtil;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author fang
 * @since 2021-02-25
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private PaintingWorksService paintingWorksService;
    @Autowired
    private UserPnUtil userPnUtil;
    @Autowired
    private APPMemberMapper appMemberMapper;
    @Autowired
    private AppAccountMapper appAccountMapper;
    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    @Override
    public SuperResponse memberList(MemberListRequest request) {

        SuperResponse response = new SuperResponse();
        QueryWrapper<Member> wrapper = new QueryWrapper();
        Page<Member> pages = new Page<>(request.getPageNo(),request.getPageSize());

        if(!StringUtils.isEmpty(request.getNickname())){
            wrapper.like("nickname",request.getNickname());
        }

        if(null!=request.getViolation()){
            wrapper.eq("trust_type",request.getViolation());
        }

        if(!StringUtils.isEmpty(request.getStartTime())&&!StringUtils.isEmpty(request.getEndTime())){
            wrapper.between("create_time",request.getStartTime(),request.getEndTime());
        }

        wrapper.orderByDesc("create_time");
        IPage<Member> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<Member> list = mapIPage.getRecords();
        for(Member m:list){
            m.setWorks(paintingWorksService.works(m.getUserId()));
        }
        response.setItems(list);
        response.setTotals(mapIPage.getTotal());
        return response;
    }

    @Override
    public SuperResponse info(MemberRequest request) {
        SuperResponse response  = new SuperResponse();
        Member member = baseMapper.selectById(request.getUserId());
        response.setItem(member);
        return response;
    }

    @Override
    public SuperResponse createMember(MemberInfoRequest request) {
        return null;
    }

    @Override
    public SuperResponse block(MemberRequest request) {
        SuperResponse response  = new SuperResponse();
        Member  member = findMemberByUid(request.getUserId());
        if(member.getDelFlag()==0){
            member.setDelFlag(1);
        }else{
            member.setDelFlag(0);
        }
        baseMapper.updateById(member);
        return response;
    }


    @Override
    public SuperResponse violation(ViolationRequest request) {
        SuperResponse response  = new SuperResponse();
        Member  member = findMemberByUid(request.getUserId());
        if(request.getType()==0){
            member.setNickname("昵称已重置");
        }else{
            member.setHeadImgUrl("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgeticon?seq=731683292&username=@5ebb7848ff0105560d57cce857f17be01ce013ff33347aa1461ab2b5fc6f9e09&skey=@crypt_9859c43f_dee5838fc584e59cb94c844b81bdf48b&type=big");
        }

        baseMapper.updateById(member);
        return response;
    }



    @Override
    public SuperResponse trust(MemberRequest request) {
        SuperResponse response  = new SuperResponse();

        Member  member = findMemberByUid(request.getUserId());
        if(member.getTrustType()==0){
            member.setTrustType(1);
        }else{
            member.setTrustType(0);
        }
        baseMapper.updateById(member);
        return response;
    }


    @Override
    public Member findMemberByUid(String userId) {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        return baseMapper.selectOne(wrapper);
    }


    @Override
    public int registerTotals() {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int registerToday() {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.last("WHERE TO_DAYS(create_time) = TO_DAYS(NOW())");
        return baseMapper.selectCount(wrapper);

    }

    @Override
    public SuperResponse getInventedMember(APPMemberRequest request) {
        SuperResponse response  = new SuperResponse();
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.eq("invented","T");
        response.setItems(baseMapper.selectList(wrapper));
        return response;
    }

    @Override
    public int yesterdayRegisters() {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.last("WHERE TO_DAYS(NOW( )) - TO_DAYS(create_time) = 1");
        System.out.println(wrapper.getSqlSelect());
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int weekRegisters() {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.last("WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= create_time");
        return baseMapper.selectCount(wrapper);
    }

    @Override
    public int monthRegisters() {
        QueryWrapper<Member> wrapper = new QueryWrapper();
        wrapper.last("WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= create_time");
        return baseMapper.selectCount(wrapper);
    }


    @Override
    public SuperResponse addInventedMember(APPMemberRequest request) {
        SuperResponse response = new SuperResponse();
        Member member = new Member();
        String userId = IdUtils.getUUID();
        member.setUserId(userId);
        member.setPaintingNum(userPnUtil.getPaintingNum().toString());
        member.setNickname(request.getNickName());
        member.setUsername(request.getNickName());
        member.setInvented("T");
        member.setSecondTime(DateUtil.getNowTimeStr());
        member.setLastTime(DateUtil.getNowTimeStr());
        member.setCity(request.getCity());
        member.setProvince(request.getProvince());
        member.setSex(request.getGender());
        member.setPassword(passwordEncoder.encode("123456"));
        member.setHeadImgUrl(request.getHeadUrl());
        member.setStatus(1);
        member.setTrustType(0);
        member.setIsReal("F");
        appMemberMapper.insert(member);

        Account account = new Account();
        account.setUserId(userId);
        account.setId(IdUtils.getUUID());
        account.setBalance(new BigDecimal(0.00));
        account.setFrozen(new BigDecimal(0.00));
        account.setIncome(new BigDecimal(0.00));
        appAccountMapper.insert(account);
        return response;
    }
}
